package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.api.domain.SysDatascope;
import com.ruoyi.system.api.domain.SysDatascopeData;
import com.ruoyi.system.mapper.SysDatascopeDataMapper;
import com.ruoyi.system.mapper.SysDatascopeMapper;
import com.ruoyi.system.service.ISysDatascopeDataService;

/**
 * 数据权限数据Service业务层处理
 * 
 * @author breeze
 * @date 2024-01-16
 */
@Service
public class SysDatascopeDataServiceImpl extends ServiceImpl<SysDatascopeDataMapper,SysDatascopeData> implements ISysDatascopeDataService {
    
    @Autowired
    private SysDatascopeMapper sysDatascopeMapper;
    
    @Autowired
    private SysDatascopeDataMapper sysDatascopeDataMapper;

    @Override
    public SysDatascopeData selectSysDatascopeDataByDatascopeCode(String datascopeCode){
        return sysDatascopeDataMapper.selectSysDatascopeDataByDatascopeCode(datascopeCode);
    }
      
    @Override
    public List<SysDatascopeData> selectSysDatascopeDataList(SysDatascopeData sysDatascopeData){
        return sysDatascopeDataMapper.selectSysDatascopeDataList(sysDatascopeData);
    }
    
    @Override
	public boolean saveData(SysDatascopeData sysDatascopeData) {
		Map<String,Object> query = new HashMap<String,Object>();
		query.put("code", sysDatascopeData.getDatascopeCode());
		List<SysDatascope> datascopes = sysDatascopeMapper.selectByMap(query);
		if(datascopes.size() == 1) {
			int count = sysDatascopeDataMapper.insert(sysDatascopeData);
			if(count > 0) {
				return true;
			}else {
				return false;
			}
		}else {
			return false;
		}
	}
    @Override
	public boolean removeAndSaveData(SysDatascopeData sysDatascopeData) {
    	
    	String resourceValue = sysDatascopeData.getResourceValue();
    	if(null == resourceValue || "".equals(resourceValue)) {
    		log.error("必要参数resourceValue为" + resourceValue);
    		return false;
    	}
    	String[] values;
    	if(resourceValue.indexOf(",") > 0) {
    		values = resourceValue.split(",");
    	}else {
    		values = new String[]{resourceValue};
    	}
    	
    	//先删除所有
    	Map<String,Object> params = new HashMap<String,Object>();
    	params.put("datascope_code", sysDatascopeData.getDatascopeCode());
    	params.put("owner_value", sysDatascopeData.getOwnerValue());
    	sysDatascopeDataMapper.deleteByMap(params);
    	
    	//在添加所有
    	List<SysDatascopeData> datas = new ArrayList<SysDatascopeData>();
    	for (int i = 0; i < values.length; i++) {
    		SysDatascopeData datascopeData = new SysDatascopeData();
    		datascopeData.setDatascopeCode(sysDatascopeData.getDatascopeCode());
    		datascopeData.setOwnerValue(sysDatascopeData.getOwnerValue());
    		datascopeData.setResourceValue(values[i]);
    		datas.add(datascopeData);
		}
    	
		return this.saveBatch(datas);
	}

	@Override
	public boolean removeDataByMap(SysDatascopeData sysDatascopeData) {
    	Map<String,Object> params = new HashMap<String,Object>();
    	params.put("datascope_code", sysDatascopeData.getDatascopeCode());
    	params.put("owner_value", sysDatascopeData.getOwnerValue());
    	params.put("resource_value", sysDatascopeData.getResourceValue());
		sysDatascopeDataMapper.deleteByMap(params);
		return true;
	}
}
